﻿using Bootstrapper.AssemblyProviders;
using Common.Domain.Transactions;
using Common.Infrastructure.Persistence.AssembliesProvider;
using Common.Infrastructure.Persistence.ConnectionString;
using Common.Infrastructure.Persistence.SessionFactoryBuilding;
using Common.Infrastructure.Persistence.Transactions;
using Common.Logging;
using NHibernate;
using Ninject;
using Product.Infrastructure.Persistence.SubProducts;
using Products.Application.Products;
using Products.Domain.Products;
using Products.Domain.SubProducts;
using Products.Infrastructure.Persistence.Products;
using Ninject.Web.Common;

namespace Bootstrapper
{
    public class NinjectBootsrapper
    {
        public static IKernel CreateAndConfigureContainer()
        {
            var ninjectKernel = new StandardKernel();
            AddBindings(ninjectKernel);
            return ninjectKernel;
        }

        private static void AddBindings(IKernel kernel)
        {
            kernel.Bind<IPersistenceAssemblyProvider>().To<PersistenceAssemblyProvider>();
            kernel.Bind<IConnectionStringProvider>().To<ConnectionStringProvider>();
            kernel.Bind<ITransactionExecutor>().To<TransactionExecutor>().InRequestScope();
            kernel.Bind<ISimpleLogger>().To<SimpleLogger>();
            kernel.Bind<IProductCommandService>().To<ProductCommandService>();
            kernel.Bind<IProduceFactory>().To<ProduceFactory>();
            kernel.Bind<IProductRepository>().To<NHibernateProductRepository>();
            kernel.Bind<ISubProductRepository>().To<NHibernateSubProductRepository>();
            kernel.Bind<ISessionFactoryBuilder>().To<PostgreSQLSessionFactoryBuilder>().InSingletonScope();
            kernel.Bind<ISessionFactory>().ToMethod(
                context => context.Kernel.Get<ISessionFactoryBuilder>().BuildSessionFactory()).InSingletonScope();

            kernel.Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).
                 InRequestScope();
            

        }

        
    }
}
